/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: HystrixClientFallbackFactory
 * @Package com.rx.service.base
 * @Description: 服务降级
 * @author: 陈锦韬
 * @date: 2020\4\20 0020
 * @version V1.0
 * @Copyright: 2020 陈锦韬  All rights reserved.
 */
package com.rx.zuul.service.impl;

import com.rx.core.common.SecretInfo;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.util.RxUtil;
import com.rx.zuul.bean.LoginInfoFromUua;
import com.rx.zuul.service.FeignLoginService;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: Administrator
 * @Description: 服务降级
 * @date: 2020\4\20 0020
 */
@Slf4j
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<FeignLoginService> {
    @Override
    public FeignLoginService create(Throwable cause) {
        log.info("user service exception:" + cause.getMessage());
        return new HystrixClientWithFallBackFactory(){
            @Override
            public LoginInfoFromUua getToken(@RequestParam("client_id") String clientId,
                                             @RequestParam("client_secret") String clientSecret,
                                             @RequestParam("grant_type") String grantType,
                                             @RequestParam("code") String code,
                                             @RequestParam("redirect_uri") String redirectUri,
                                             @RequestParam("username") String username,
                                             @RequestParam("password") String password){

                if (cause.getMessage().contains("Rsa密码解密失败")){
                    LoginServiceImpl.CLIENT_SECRET = "";
                    RxUtil.CheckUtil.throwOut(RESULT_CODE.LOGIN_ERR,"用户名或密码错误");
                }else if (cause.getMessage().contains("用户口令错误")){
                    RxUtil.CheckUtil.throwOut(RESULT_CODE.LOGIN_ERR,"用户名或密码错误");
                }else if(cause.getMessage().contains("绑定的用户不唯一")){
                    RxUtil.CheckUtil.throwOut(RESULT_CODE.LOGIN_ERR,"绑定的用户不唯一,请用另一种方式登录");
                }
                LoginServiceImpl.CLIENT_SECRET = "";
                //return RxResponse.fail(RESULT_CODE.LOGIN_ERR,"用户名或密码错误");
                RxUtil.CheckUtil.throwOut(RESULT_CODE.LOGIN_ERR,"用户名或密码错误");
                return null;
            }
            @Override
            public SecretInfo secret() {
                LoginServiceImpl.CLIENT_SECRET = "";
                return null;
            }
        };
    }
}
